package com.misc.tunewsreader;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filterable;
import android.widget.TextView;

public class ThreadAdapter extends ArrayAdapter<NNTPThreadInfo> implements
		Filterable {

	private List<NNTPThreadInfo> items;
	private int layout_id;
	private Context context;

	public ThreadAdapter(Context context, int textViewResourceId,
			List<NNTPThreadInfo> threads) {
		super(context, textViewResourceId, threads);
		this.items = threads;
		this.context = context;
		this.layout_id = textViewResourceId;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			view = inflater.inflate(layout_id, parent, false);
		}
		NNTPThreadInfo tinfo = items.get(position);
		if (tinfo != null) {
			TextView title_text = (TextView) view.findViewById(R.id.subject);
			TextView author_text = (TextView) view.findViewById(R.id.author);
			TextView thread_id = (TextView) view.findViewById(R.id.threadid);
			TextView date_text = (TextView) view.findViewById(R.id.date);
			if (title_text != null) {
				title_text.setText(Html.fromHtml(tinfo.getSubject()));
			}
			if (author_text != null) {
				author_text.setText(tinfo.getAuthor());
			}
			if (thread_id != null) {
				thread_id.setText("" + tinfo.getThreadId());
			}
			if (date_text != null) {
				date_text.setText(tinfo.getDate());
			}

		}
		return view;
	}

	public NNTPThreadInfo getItem(int position) {
		return items.get(position);
	}

	public int getPosition(NNTPThreadInfo item) {
		return items.indexOf(item);
	}

	public long getItemId(int position) {
		return position;
	}

	public int getCount() {
		return items.size();
	}
}
